package com.sgb.kjwl.view.ui.activity.self.order;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityChangeOrderPeopleBinding;
import com.sgb.kjwl.di.order.ChangeOrderPeopleModule;
import com.sgb.kjwl.di.order.DaggerChangeOrderPeopleComponent;
import com.sgb.kjwl.model.entity.ChangeOrderPeopleEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.sgb.kjwl.view.ui.adapter.self.order.ChangeOrderPeopleAdapter;
import com.sgb.kjwl.viewmodel.self.ChangeOrderPeopleModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class ChangeOrderPeopleActivity extends AppActivity {

    private ActivityChangeOrderPeopleBinding binding;
    @Inject
    ChangeOrderPeopleModel viewModel;

    private ChangeOrderPeopleAdapter changeOrderPeopleAdapter;
    private int peoplePosition;
    private List<UnitEntity> unitEntityList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerChangeOrderPeopleComponent.builder().appComponent(appComponent).changeOrderPeopleModule(new ChangeOrderPeopleModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_change_order_people);
        binding.include.topLayout.setBackgroundColor(getResources().getColor(R.color.color_FFFFFF));
        binding.include.titleView.setText("变更跟单人");
        binding.include.rightView.setVisibility(View.INVISIBLE);
        binding.tvChoosePeople.setEnabled(false);
        binding.include.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        if (changeOrderPeopleAdapter == null) {
            changeOrderPeopleAdapter = new ChangeOrderPeopleAdapter(this, null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        binding.xrvChangeOrderPeople.setLayoutManager(layoutManager);
        binding.xrvChangeOrderPeople.setAdapter(changeOrderPeopleAdapter);

        binding.xrvChangeOrderPeople.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData();
            }

            @Override
            public void onLoadMore() {
                viewModel.getOrderList(true);
            }
        });

        changeOrderPeopleAdapter.setOnItemClick(new AdapterOnItemClick<ChangeOrderPeopleEntity.ListBean>() {
            @Override
            public void onItemClick(ChangeOrderPeopleEntity.ListBean listBean, int position) {
                changeOrderPeopleAdapter.getDatas().get(position).setSelect(!changeOrderPeopleAdapter.getDatas().get(position).isSelect());
                changeOrderPeopleAdapter.notifyDataSetChanged();
                refreshButton();
            }
        });

    }

    private void refreshButton() {
        boolean selected = false;
        for (ChangeOrderPeopleEntity.ListBean entity : changeOrderPeopleAdapter.getDatas()){
            if (entity.isSelect()){
                selected = true;
                break;
            }
        }

        if (selected){
            binding.tvChoosePeople.setBackground(getResources().getDrawable(R.drawable.corner_blue_button_bg));
            binding.tvChoosePeople.setEnabled(true);
        }else {
            binding.tvChoosePeople.setBackground(getResources().getDrawable(R.drawable.corner_gery_button_bg));
            binding.tvChoosePeople.setEnabled(false);
        }
    }

    @Override
    protected void init() {
        //viewModel.getOrderList(false);
        binding.tvChoosePeople.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewModel.getPeopleModel();
            }
        });
    }

    private void initData() {
        viewModel.getOrderList(false);
    }

    /**
     * 展示订单列表
     * @param datas
     * @param isLoadMore
     */
    public void showOrderList(List<ChangeOrderPeopleEntity.ListBean> datas, boolean isLoadMore) {
        if (isLoadMore) {
            changeOrderPeopleAdapter.addDatas(datas);
            binding.xrvChangeOrderPeople.loadMoreComplete();
            if (changeOrderPeopleAdapter.getItemCount() >= viewModel.total) {
                binding.xrvChangeOrderPeople.setLoadingMoreEnabled(false);
            }
        } else {
            changeOrderPeopleAdapter.setDatas(datas);
            binding.xrvChangeOrderPeople.refreshComplete();
            if (datas.size()==0){
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.tvChoosePeople.setVisibility(View.GONE);
            }else {
                binding.emptyView.setVisibility(View.GONE);
                binding.tvChoosePeople.setVisibility(View.VISIBLE);
            }
        }
    }

    public void showPeople(List<UnitEntity> unitEntityList) {
        this.unitEntityList = unitEntityList;
        showPeoplePop();
    }

    /**
     * 选择跟单人弹窗
     */
    private void showPeoplePop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(ChangeOrderPeopleActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                /*String content = unitEntityList.get(options1).getName();
                binding.tvUnit.setText(content);*/
                peoplePosition = options1;
            }
        }).setDecorView(getWindow().getDecorView().findViewById(android.R.id.content)).build();
        businessOption.setPicker(unitEntityList);
        businessOption.setSelectOptions(peoplePosition);
        businessOption.show();
    }

}
